[img] Basic Line Editor
 MDFS::Info.Comp.Spectrum.Code.Editors.lineedit/htm Search  

The Spectrum INPUT command uses the bottom of the screen. On most other platforms, you can use INPUT
anywhere on the screen. This code provides a simple line editor to allow you to enter characters with the input
line displayed anywhere in the main screen.

DELETE deletes the previous character, ENTER returns the current line. All other control codes are ignored. All
symbol characters can be entered with the "Symbol Shift" layout by pressing Symbol-Shift and the relevant key,
and © is entered with Symbol-Shift-I. The text is displayed where-ever the current cursor is, so you can display
a prompt before calling, for instance PRINT "Enter filename: ";:GOSUB 9500


This allows no limit to the number of characters entered.

Call with:   GOSUB 9500
Returns:    a$ holds the text after ENTER is pressed, k$ corrupted
Download: lineedit.tap (4K).

9500 REM Simple line editor, returns a$=text entered
9505 LET a$=""
9520 PRINT CHR$143;CHR$8;
9530 PAUSE 0:LET k$=INKEY$:IF k$="" THEN GOTO 9530
9535 IF k$=CHR$6 THEN IF PEEK 23658<9 THEN POKE 23658,8-PEEK 23658:GOTO 9520
9540 IF k$=CHR$12 THEN IF LEN a$ THEN LET a$=a$(TO LENa$-1):PRINT " ";CHR$8;CHR$8;:GOTO 9520
9550 IF k$=CHR$13 THEN PRINT " ":RETURN
9570 IF k$>CHR$171 THEN LET k$=(CHR$127+"|!][QEW!}{\~")(CODEk$-193+22*(k$<CHR$195)-20*(k$>CHR$205))
9580 IF k$>=" " THEN LET a$=a$+k$:PRINT k$;
9590 GOTO 9520

This variant allows you to specify a maximum line length.

Call with:   GOSUB 9500
or             LET max=<linelength>:GOSUB 9505
Returns:    a$ holds the text after ENTER is pressed, k$ corrupted, max preserved
Download: lineedit.tap (4K).

9500 REM Simple line editor, returns a$=text entered, allows max to be set
9504 LET max=768
9505 LET a$=""
9520 PRINT CHR$143;CHR$8;
9530 PAUSE 0:LET k$=INKEY$:IF k$="" THEN GOTO 9530
9535 IF k$=CHR$6 THEN IF PEEK 23658<9 THEN POKE 23658,8-PEEK 23658:GOTO 9520
9540 IF k$=CHR$12 THEN IF LEN a$ THEN LET a$=a$(TO LENa$-1):PRINT " ";CHR$8;CHR$8;:GOTO 9520
9550 IF k$=CHR$13 THEN PRINT " ":RETURN
9560 IF LEN a$>=max THEN GOTO 9530
9570 IF k$>CHR$171 THEN LET k$=(CHR$127+"|!][QEW!}{\~")(CODEk$-193+22*(k$<CHR$195)-20*(k$>CHR$205))
9580 IF k$>=" " THEN LET a$=a$+k$:PRINT k$;
9590 GOTO 9520

This allows you to specify a maximum line length and a pad character.

Call with:   GOSUB 9500
or             LET max=<linelength>:GOSUB 9505
or             LET max=<linelength>:LET pad=<character>:GOSUB 9510
Returns:    a$ holds the text after ENTER is pressed, k$, pad corrupted, max preserved
Download: lineedit.tap (4K).

9500 REM Simple line editor, returns a$=text entered, allows max and pad to be set
9504 LET max=768
9505 LET pad=0
9510 LET a$=""
9518 IF pad THEN FOR a=1 TO max:PRINT CHR$pad;:NEXT a:FOR a=1 TO max:PRINT CHR$8;:NEXT a
9519 IF pad=0 THEN LET pad=32
9520 PRINT CHR$143;CHR$8;
9530 PAUSE 0:LET k$=INKEY$:IF k$="" THEN GOTO 9530
9535 IF k$=CHR$6 THEN IF PEEK 23658<9 THEN POKE 23658,8-PEEK 23658:GOTO 9520
9540 IF k$=CHR$12 THEN IF LEN a$ THEN PRINT (CHR$pad+" ")(1+(LENa$=max));CHR$8;CHR$8;:LET a$=a$(TO LENa$-1):GOTO 9520
9550 IF k$=CHR$13 THEN PRINT (CHR$pad+" ")(1+(LENa$=max)):RETURN
9560 IF LEN a$>=max THEN GOTO 9530
9570 IF k$>CHR$171 THEN LET k$=(CHR$127+"|!][QEW!}{\~")(CODEk$-193+22*(k$<CHR$195)-20*(k$>CHR$205))
9580 IF k$>=" " THEN LET a$=a$+k$:PRINT k$;
9590 GOTO 9520

To show the state of the CAPS LOCK, change line 9520 to:
9520 PRINT FLASH 1;CHR$(76-(PEEK 23658)/8*9);CHR$8;

To give a keypress beep, add:
9539 BEEP 0.0005,34.35


Best viewed with Any Browser Valid HTML 4.0! Authored by J.G.Harston
Last update: 12-Nov-2019